package com.lxd.api.filter.Impl;

import com.lxd.api.feign.cacheClient;
import com.lxd.api.filter.CheckFilter;
import com.lxd.common.constants.CacheConstant;
import com.lxd.common.enums.ExceptionEnums;
import com.lxd.common.exception.ApiException;
import com.lxd.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service(value = "ip")
@Slf4j
public class IPCheckFilter implements CheckFilter {
    @Autowired
    private cacheClient cacheClient;

    private final String IP_ADDRESS="ipAddress";

    @Override
    public void check(StandardSubmit submit) {
        log.info("【接口模块-校验IP】   校验ing…………");

        //1.根据 apikey ipaddress 查询ip
        String ip =cacheClient.hgetString(CacheConstant.CLIENT_BUSINESS + submit.getApikey(), IP_ADDRESS);
        log.info("【接口模块-校验IP】ip={}",ip);
        //2.若为空，说明第一次访问 ，放行     若不为空，查看是否包含请求的ip，如果包含，说明可信任，放行
        if (StringUtils.isEmpty(ip)||ip.contains(submit.getIp())){
            log.info("【接口模块-校验IP】 IP可以放行！");
            return;
        }
        //3.不放行，抛出异常
//        log.info("【接口模块-校验IP】 IP校验失败，不能放行！");
//        throw new ApiException(ExceptionEnums.IP_NOT_WHITE);


    }
}


